home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / science / sm32a.zip / SYMBMATH.H41 < prev    next >
Text File  |  1993-11-07  |  7KB  |  195 lines

  1.         4.9   Arrays, Lists, Vectors and Matrices
  2.     You can construct arrays and lists of arbitrary length, and the 
  3. entries in the arrays and lists can be of any type of value whatsoever: 
  4. constants, expressions with undefined variables, or equations.
  5.     A vector and matrix can be represented by a list or array. In a
  6. matrix, the number of elements in each row should be the same, e.g.
  7. [[a11, a12], [a21, a22]].
  8.  
  9.                 4.9.1    Arrays
  10.                 4.9.1.1  Entering Arrays
  11.         You can define an array of a by assigning its element value into
  12. its index:
  13.                 a[1]:=1
  14.                 a[2]:=4
  15. or you can define arrays another way, with the command:
  16.         do(a[x]:=f(x), x from xmin to xmax step dx)
  17.         e.g.
  18.                 do(a[j] := 2*j, j from 1 to 2)
  19.         You can define 2-dimentional array by
  20.                 a[1,1]:=11
  21.                 a[1,2]:=12
  22.                 a[2,1]:=21
  23.                 a[2,2]:=22
  24. or
  25.        do(do(a[j,k]:=j+k, j,jmin,jmax,dj), k,kmin,kmax,dk)
  26.  
  27.                 4.9.1.2  Accessing Arrays
  28.         After defining an array of a, you can access one of its element
  29. by its index
  30.  
  31. IN:  a[1]
  32. OUT: 1
  33.  
  34. you also can list out all of its elements by
  35.                list(a[j], j,1,2,1)
  36.         e.g.
  37. IN:  do(a[j]:=2*j, j,1,2,1)
  38. IN:  list(a[j], j,1,2)
  39. OUT: 1, 4
  40.  
  41.                 4.9.1.3  Modifying Arrays
  42.         You can modify an array by assigning new value into its index
  43.  
  44. IN:  a[1]:=2
  45.  
  46.                 4.9.1.4  Operating Arrays
  47.         e.g.
  48.         after defining 2 arrays a and b, find their dot time, a .* b.
  49. IN:  a[1]:=1, a[2]:=2                 # define array a
  50. IN:  b[1]:=11, b[2]:=12               # define array b
  51. IN:  p:=0
  52. IN:  do(p:=p + a[j]*b[j], j,1,2,1)    # a .* b
  53.  
  54.                   4.9.2    Lists
  55.           4.9.2.1  Entering Lists
  56.     You can define a list by putting its elements between two square
  57. brackets. e.g.        [1,2,3]
  58.     You can define lists another way, with the command:
  59.            [ list(f(x), x from xmin to xmax step dx) ]
  60. This is similar to the sum command,  but the result is a list:
  61.            [f(xmin), f(xmin+dx), ..., f(xmin+x*dx), ...]
  62. which continues until the last value of xmin + x*dx  <= xmax.  
  63.        You also can assign the list to a variable, which variable name
  64. become the list name:
  65.  
  66.          a := [1,2,3]                 # define the list of a
  67.          b := [f(2), g(1), h(1)]      # assumes f,g,h defined
  68.          c := [[1,2],3,[4,5]]         # define the list of c
  69.  
  70. Lists are another kind of value in SymbMath, and they can be assigned 
  71. to variables just like simple values.  (Since variables in SymbMath 
  72. language are untyped, you can assign any value to any variable.).
  73.     A function can have a list for its value:
  74.          f(x_) := [sqrt(x), -sqrt(x)]
  75.         e.g.
  76. IN:  squreroot(x_) := [sqrt(x), -sqrt(x)]
  77. IN:  squreroot(4)
  78. OUT: [2, -2]
  79.  
  80.         A function can have a list for its arguement:
  81.              abs([-1,2])
  82.     Try
  83.          a := [ list(j^2, j from 0 to 10 step 1) ]
  84.          f(x_) := [ list(x^j, j from 0 to 6 step 1) ]
  85.          b := f(-2)
  86.  
  87.           4.9.2.2     Accessing Lists
  88.  
  89.     You can find the value of the j-th member in a list by
  90.                   member([a,b], j)
  91. The first member of a list is always member(x, 1).
  92.         If you have assigned a list to a variable x, you can access the
  93. j-th element by the list index x[j]. The first element of x
  94. is always  x[1].  If the x[j] itself is a list, then its j-th element is
  95. accessed by repeating the similar step.    But you can not use the list
  96. index unless the list is already assigned to x.
  97.  
  98.         e.g.
  99. IN:  x := [[1,2],3,[4,5]]     # define the x list
  100. IN:  x[1], x[2]               # take its first and 2nd element
  101. OUT: [1, 2], 3
  102. IN:  x                        # access the entire list of x
  103. OUT: [[1, 2], 3, [4,5]]
  104. IN:  member(x, 2)             # same as x[2]
  105. OUT: 3
  106.  
  107.     An entire sub-list of a list x  can be accessed with the 
  108. command x[j], which is the list:
  109.           [x[j], x[j+1], ... ]
  110.  
  111.  
  112.           4.9.2.3     Modifying Lists
  113.  
  114.     The subs() replaces the value of the element in
  115. the list, as in the variables. e.g.
  116.  
  117. IN:  subs([a,b,c], a = a0)
  118. OUT: [a0, b, c]
  119.  
  120.         Note that you cannot modify lists by assignment.
  121.  
  122.           4.9.2.4     Operating Lists
  123.  
  124.     Lists can be added, subtracted, multiplied, and divided by 
  125. other lists or by constants.  When two lists are combined, they are
  126. combined term-by-term, and the combination stops when the shortest 
  127. list is exhausted.  When a scalar is combined with a list, it is 
  128. combined with each element of the list.  Try:
  129.  
  130.      a := [1,2,3]
  131.      b := [4,5,6]
  132.      a + b
  133.      a / b
  134.      3 * a
  135.      b - 4
  136.  
  137.     Example 4.9.2.4.1.
  138.         Two lists are added.
  139.  
  140. IN:  [a1,a2,a3] + [b1,b2,b3]
  141. OUT: [a1 + b1, a2 + b2, a3 + b3]
  142. IN:  last[1]
  143. OUT: a1 + b1
  144.  
  145.     If L is a list, then  f(L) results in a list of the values,
  146. even though f() is the differentiation or integration function (d() or 
  147. inte()). 
  148.  
  149. IN:  sqrt([a, b, c])
  150. OUT: [sqrt(a), sqrt(b), sqrt(c)]
  151. IN:  d([x, x^2, x^3], x)
  152. OUT: [1, 2*x, 3*x^2]
  153.  
  154.     If you use a list as the value of a variable in a function,
  155. SymbMath will try to use the list in the calculation.
  156.     You can sum all the elements in a list x by
  157.                listsum(x)
  158.     Example:
  159.  
  160. IN:  listsum([a,b,c]^2)
  161. OUT: a^2 + b^2 + c^2
  162.  
  163.     This function takes the sum of the squares of all the elements 
  164. in the list x.
  165.     You can do other statistical operations (see Section 4.10. 
  166. Statistics) on the list, or plot the list of numeric data (see
  167. Section 5. Plot).
  168.     You can find the length of a list (the number of elements in a
  169. list) with:
  170.         length(a)
  171.  
  172.           4.9.3     Vectors and Matrices
  173.     You can uses arrays or lists to represent vectors, and lists of
  174. lists to represent matrices.
  175.     Vectors and matrices can be operated by "+" and "-" with vectors
  176. and matrixes, by "*" and "/" with a scalar, and by diff() and inte().
  177. These operations are on each element, as in lists.
  178.     You can use lists as vectors, adding them and multiplying them 
  179. by scalars. For example, the dot product of two vectors of a and b is:
  180.             sum(a[j]*b[j], j from 1 to jmax)
  181.     You can even make this into a function:
  182.             dottime(x_, y_) := listsum(x*y)
  183.         e.g.
  184.         represnt the dot product of two vectors by arrays
  185. IN:  a[1]:=1, a[2]:=2                 # define array a
  186. IN:  b[1]:=11, b[2]:=12               # define array b
  187. IN:  p:=0
  188. IN:  do(p:=p + a[j]*b[j], j,1,2,1)    # a .* b
  189.  
  190.         represnt the dot product of two vectors by lists
  191. IN:  dottime([1,2], [11,12])          # by lists in function dottime()
  192.  
  193.     How about the cross product:
  194. cross(a,b) = [a[2]*b[3]-b[2]*a[3],a[3]*b[1]-b[3]*a[1],a[1]*b[2]-b[1]*a[2]]
  195.